﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ProductManager
{
    public partial class CategoryManager : Form
    {
        List<string[]> _categoryTree = new List<string[]>();
        private string _currentParentID = "/";
        private string _currentPath = "[Root]";

        public CategoryManager()
        {
            InitializeComponent();
            _categoryTree.Add(new string[] { "/", "/" });
            lblPath.Text = _currentPath;
        }

        private DataTable GetChildCategory(string parentID)
        {
            if (parentID == "/")
            {
                return DBAccess.GetRootCategory();
            }
            else
            {
                return DBAccess.GetChildCategory(parentID);
            }
        }

        private void RefreshParentCategory()
        {
            DataTable child;
            string indent = "";

            lvParent.Items.Clear();
            foreach (string[] category in _categoryTree)
            {
                lvParent.Items.Add(new ListViewItem(category));
            }
            if (_categoryTree.Count > 1)
            {
                indent = "|--";
                for (int i = 0; i < _categoryTree.Count - 2; i++)
                {
                    indent = "    " + indent;
                }
            }
            child = GetChildCategory(_categoryTree[_categoryTree.Count - 1][1]);
            foreach (DataRow row in child.Rows)
            {
                lvParent.Items.Add(new ListViewItem(new string[] {
                    indent + row["Name"].ToString(), row["ID"].ToString() }));
            }
        }

        private void RefreshChildCategory()
        {
            DataTable child = GetChildCategory(_currentParentID);
            lvChild.Items.Clear();
            foreach (DataRow row in child.Rows)
            {
                lvChild.Items.Add(new ListViewItem(new string[] { row["Name"].ToString(), row["ID"].ToString() }));
            }
        }

        public void AddExistCategory(string id)
        {
            DBAccess.AddChildCategory(_currentParentID, id);
            RefreshParentCategory();
            RefreshChildCategory();
        }

        private void CategoryManager_Load(object sender, EventArgs e)
        {
            RefreshParentCategory();
        }

        private void lvParent_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvParent.SelectedIndices.Count == 1)
            {
                if (lvParent.SelectedIndices[0] >= _categoryTree.Count - 1)
                {
                    _currentParentID = lvParent.SelectedItems[0].SubItems[1].Text;
                    RefreshChildCategory();
                }
            }
        }

        private void RefreshCurrentPath(String item)
        {
            if (item == "/")
                _currentPath = "[Root]";
            else if (item == "up")
            {
                for (int i = _currentPath.Length; i > 0; i--)
                {
                    _currentPath = _currentPath.Remove(i - 1, 1);
                    if (_currentPath[i - 2].ToString() == "/")
                    {
                        _currentPath = _currentPath.Remove(i - 2, 1);
                        break;
                    }
                }
            }
            else
            {
                if (item.IndexOf("|--") != -1)
                    item = item.Remove(0, item.IndexOf("|--") + 3);
                _currentPath = _currentPath + "/" + item;
            }
            lblPath.Text = _currentPath;
        }

        private void lvParent_DoubleClick(object sender, EventArgs e)
        {
            if (lvParent.SelectedIndices.Count == 1)
            {
                
                if (lvParent.SelectedIndices[0] < _categoryTree.Count)
                {
                    while (_categoryTree.Count - 1 > lvParent.SelectedIndices[0])
                    {
                        _categoryTree.RemoveAt(_categoryTree.Count - 1);
                        RefreshCurrentPath("up");
                    }
                }
                else
                {
                    RefreshCurrentPath(lvParent.SelectedItems[0].SubItems[0].Text);
                    _categoryTree.Add(new string[] {
                        lvParent.SelectedItems[0].SubItems[0].Text,
                        lvParent.SelectedItems[0].SubItems[1].Text });
                }
                RefreshParentCategory();
            }
            else
            {
                MessageBox.Show("Please choose one parent category!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (txtName.Text != "")
            {
                if (DBAccess.AddCategory(txtName.Text))
                {
                    DataTable lastCategory = DBAccess.GetLastRow("Category");
                    if (lastCategory.Rows.Count > 0 && _currentParentID != "/")
                    {
                        DBAccess.AddChildCategory(_currentParentID,
                            lastCategory.Rows[0]["ID"].ToString());
                    }
                    RefreshParentCategory();
                    RefreshChildCategory();
                }
                else
                {
                    MessageBox.Show("Add category failed!!!");
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddCategory frmNew = new AddCategory();
            frmNew.ShowDialog(this);
        }

        private void btnRename_Click(object sender, EventArgs e)
        {
            if (lvChild.SelectedItems.Count == 1)
            {
                if (DBAccess.RenameCategory(lvChild.SelectedItems[0].SubItems[1].Text, txtName.Text))
                {
                    RefreshParentCategory();
                    RefreshChildCategory();
                }
                else
                {
                    MessageBox.Show("Rename category failed!!!");
                }
            }
            else
            {
                MessageBox.Show("Please choose a child category!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lvChild.SelectedItems.Count == 1)
            {
                if (DBAccess.DeleteCategory(lvChild.SelectedItems[0].SubItems[1].Text))
                {
                    RefreshParentCategory();
                    RefreshChildCategory();
                    txtName.Text = "";
                }
                else
                {
                    MessageBox.Show("Delete category failed!!!");
                }
            }
            else
            {
                MessageBox.Show("Please choose a child category!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void lvChild_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvChild.SelectedItems.Count == 1)
            {
                txtName.Text = lvChild.SelectedItems[0].SubItems[0].Text;
            }
        }
    }
}
